package filter;

import pojo.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter("/*")
public class SystemFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        String uri = request.getRequestURI();
        if (uri.contains("login.jsp") || uri.contains("checkCode.jpg") || uri.contains("userServlet") || uri.contains("index.jsp")) {
            //System.out.println("come....");
            filterChain.doFilter(req, resp);
        }
        if (uri.contains("areaCheckedNotes.jsp") || uri.contains("AreaNotes.jpg") || uri.contains("areaUsers") || uri.contains("managerAreaCheckedNotes.jsp") || uri.contains("managerAreaNotes.jsp")) {
            filterChain.doFilter(req, resp);
        } else {
            User userLogin = (User) request.getSession().getAttribute("userLogin");
            if (0 < userLogin.getMid() && userLogin.getMid() < 8) {
                filterChain.doFilter(req, resp);
            } else {
                request.setAttribute("tips", "您没有权限");
                request.getRequestDispatcher("/error.jsp").forward(request, resp);
            }
        }
    }

    @Override
    public void destroy() {

    }
}
